from django.db import models
from django.utils.html import format_html
# Create your models here.


# 创建产品分类表
class Type(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='类型ID')
    type_name = models.CharField(max_length=20, default='', verbose_name='类型名称')

    class Meta:
        verbose_name = '类型信息'
        verbose_name_plural = verbose_name

    # 设置返回值，若不设置，则默认返回Type对象
    def __str__(self):
        return self.type_name


# 创建产品信息表
class Product(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='产品ID')
    name = models.CharField(max_length=50, default='', verbose_name='产品名称')
    weight = models.CharField(max_length=20, default='', verbose_name='产品重量')
    size = models.CharField(max_length=20, default='', verbose_name='产品尺寸')
    type = models.ForeignKey(Type, on_delete=models.CASCADE, verbose_name='产品类型')   # 因为此处type指向Type模型，此处为外键，因此必须在Type设置__str__方法
    description = models.CharField(max_length=800, default='', verbose_name='描述')

    class Meta:
        verbose_name = '产品信息'
        verbose_name_plural = verbose_name

        # 自定义权限，这是新增的权限，记住格式参照auth_permission表中的codename和name，不能瞎弄
        permissions = (
            # auth_permission表中的codename和name
            ('Visit_Product', 'Can visit Product'),
        )

    # 设置返回值
    def __str__(self):
        return self.name

    # 自定义函数，设置字体的颜色
    def colored_type(self):
        if '手机' in self.type.type_name:
            color_code = 'red'
        elif '平板电脑' in self.type.type_name:
            color_code = 'blue'
        elif '智能穿戴' in self.type.type_name:
            color_code = 'green'
        else:
            color_code = 'yellow'
        return format_html(
            '<span style="color:{};">{}</span>',
            color_code,
            self.type,
        )

    # 设置新增字段colored_type在admin后台的标题
    colored_type.short_description = '带颜色的产品类型'



# 一对一关系
# class Performer(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.CharField(max_length=20, default='')
#     nationality = models.CharField(max_length=20, default='')
#
#
# class Program(models.Model):
#     id = models.IntegerField(primary_key=True)
#     performer = models.OneToOneField(Performer, on_delete=models.CASCADE)   # 这里就是说表演者的id是两者都可以使用的
#     name = models.CharField(max_length=20, default='')


# 一对多关系，一般是通过外键来关联的
# class Performer(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.CharField(max_length=20, default='')
#     nationality = models.CharField(max_length=20, default='')
#
#
# class Program(models.Model):
#     id = models.IntegerField(primary_key=True)
#     performer = models.ForeignKey(Performer, on_delete=models.CASCADE)  # 也就是Performer的id在这个表里是可以重复的
#     name = models.CharField(max_length=20, default='')


# # 多对多关系，一般是通过外键来关联的
# class Performer(models.Model):
#     id = models.IntegerField(primary_key=True)
#     name = models.CharField(max_length=20, default='')
#     nationality = models.CharField(max_length=20, default='')
#
#
# class Program(models.Model):
#     id = models.IntegerField(primary_key=True)
#     performer = models.ManyToManyField(Performer, on_delete=models.CASCADE)  # 也就是Performer的id在这个表里是可以重复的
#     name = models.CharField(max_length=20, default='')
